<template>
	<view class="summary">
		<view class="bctj">
			<text class="t-icon t-icon-shixinchaxun text-icon"></text>
			<text class="tjtitle">选择订餐月份查看记录</text>
			<view class="tjtime">
				<picker mode="date" :value="dateValue" fields="month" @change="onDateChange">
					<view class="picker">
						<text>{{ dateValue }}</text>
						<uni-icons type="down" size="20" color="#000"></uni-icons>
					</view>
				</picker>
			</view>
		</view>
		<!-- <view class="sort-time">
			<uni-icons type="calendar-filled" size="20" color="#2698fb" style="margin-left: 10rpx;"></uni-icons>
			<picker mode="date" class="select-month" :value="dateValue" fields="month" @change="onDateChange">
				<view class="picker">
					<text>选择订餐月份查看记录：{{ dateValue }}</text>
					<uni-icons type="down" size="20" color="#000"></uni-icons>
				</view>
			</picker>
		</view> -->
		<!-- table -->
		<!-- <view class="table">
			<view class="tabtr">
				<view class="tabtd">类型</view>
				<view class="tabtd">次数</view>
			</view>
			<view class="tabtr">
				<view class="tabtd">早餐订餐</view>
				<view class="tabtd">{{ breakfastOrdered }}</view>
			</view>
			<view class="tabtr">
				<view class="tabtd">中餐订餐</view>
				<view class="tabtd">{{ lunchOrdered }}</view>
			</view>
			<view class="tabtr">
				<view class="tabtd">晚餐订餐</view>
				<view class="tabtd">{{ dinnerOrdered }}</view>
			</view>
			<view class="tabtr">
				<view class="tabtd">早餐用餐</view>
				<view class="tabtd">{{ breakfastEaten }}</view>
			</view>
			<view class="tabtr">
				<view class="tabtd">中餐用餐</view>
				<view class="tabtd">{{ lunchEaten }}</view>
			</view>
			<view class="tabtr">
				<view class="tabtd">晚餐用餐</view>
				<view class="tabtd">{{ dinnerEaten }}</view>
			</view>
		</view> -->


		<!-- 报餐统计 -->
		<view style="margin: 20rpx;">

			<uni-section title="报餐汇总" type="line">
				<uni-collapse accordion>
					<uni-collapse-item title="订餐统计"
						thumb="https://limsapi.gzhbcss.com/static/tongji.png">
						<!-- 详情 -->
						<view class="rows" v-if="fooddietlist.length > 0">
							<view class="col">日期</view>
							<block v-for="(menu, mIndex) in menu" :key="mIndex">
								<view class="menu col">
									{{ menu.name }}
								</view>
							</block>
						</view>
						<view class="row" v-for="(item,index) in fooddietlist" :key="index">
							<view class="col">
								{{item.dutyDate.slice(5)}}
								<view style="color: #006400;">{{item.dayName}}</view>
							</view>
							<block v-for="(val, key, i) in item" :key="i">
								<template v-if="key != 'dutyDate' && key != 'dayName'">
									<view class="menu col">
										<view v-if="key == 'breakfastOrdered' && val" style="color: #006400;">已订</view>
										<view v-if="key == 'lunchOrdered' && val" style="color: #006400;">已订</view>
										<view v-if="key == 'dinnerOrdered' && val" style="color: #006400;">已订</view>
									</view>
								</template>
							</block>
						</view>
						<!-- 餐数 -->
						<view class="bccs">
							<view class="sum">
								<view class="bold-num">{{ breakfastOrdered }}</view>
								<view>早餐订餐</view>
							</view>
							<view class="sum">
								<view class="bold-num">{{ lunchOrdered }}</view>
								<view>中餐订餐</view>
							</view>
							<view class="sum">
								<view class="bold-num">{{ dinnerOrdered }}</view>
								<view>晚餐订餐</view>
							</view>
						</view>
					</uni-collapse-item>
					<uni-collapse-item title="用餐统计"
						thumb="https://limsapi.gzhbcss.com/static/tongji.png">
						<!-- 详情 -->
						<view class="rows" v-if="meallist.length > 0">
							<view class="col">日期</view>
							<block v-for="(menu, mIndex) in menu" :key="mIndex">
								<view class="menu col">
									{{ menu.name }}
								</view>
							</block>
						</view>
						<view class="row" v-for="(item,index) in meallist" :key="index">
							<view class="col">
								{{item.dutyDate.slice(5)}}
								<view style="color: #006400;">{{item.dayName}}</view>
							</view>
							<block v-for="(val, key, i) in item" :key="i">
								<template v-if="key != 'dutyDate' && key != 'dayName'">
									<view class="menu col">
										<view v-if="key == 'breakfastEaten' && val" style="color: #006400;">已用</view>
										<view v-if="key == 'lunchEaten' && val" style="color: #006400;">已用</view>
										<view v-if="key == 'dinnerEaten' && val" style="color: #006400;">已用</view>
									</view>
								</template>
							</block>
						</view>
						<!-- 餐数 -->
						<view class="bccs">
							<view class="sum">
								<view class="bold-num">{{ breakfastEaten }}</view>
								<view>早餐用餐</view>
							</view>
							<view class="sum">
								<view class="bold-num">{{ lunchEaten }}</view>
								<view>中餐用餐</view>
							</view>
							<view class="sum">
								<view class="bold-num">{{ dinnerEaten }}</view>
								<view>晚餐用餐</view>
							</view>
						</view>
					</uni-collapse-item>
					<uni-collapse-item title="已报餐未用统计"
						thumb="https://limsapi.gzhbcss.com/static/tongji.png">
						<!-- 详情 -->
						<view class="rows" v-if="mealnousedlist.length > 0">
							<view class="col">日期</view>
							<block v-for="(menu, mIndex) in menu" :key="mIndex">
								<view class="menu col">
									{{ menu.name }}
								</view>
							</block>
						</view>
						<view class="row" v-for="(item,index) in mealnousedlist" :key="index">
							<view class="col">
								{{item.dutyDate.slice(5)}}
								<view style="color: #006400;">{{item.dayName}}</view>
							</view>
							<block v-for="(val, key, i) in item" :key="i">
								<template v-if="key != 'dutyDate' && key != 'dayName'">
									<view class="menu col">
										<view v-if="key == 'breakfastEaten' && val" style="color: #FF0000;">未用</view>
										<view v-if="key == 'lunchEaten' && val" style="color: #FF0000;">未用</view>
										<view v-if="key == 'dinnerEaten' && val" style="color: #FF0000;">未用</view>
									</view>
								</template>
							</block>
						</view>
						<!-- 次数 -->
						<view class="bccs">
							<view class="sum">
								<view class="bold-num">{{ mealnousedBreakfastEaten }}</view>
								<view>早餐次数</view>
							</view>
							<view class="sum">
								<view class="bold-num">{{ mealnousedLunchEaten }}</view>
								<view>中餐次数</view>
							</view>
							<view class="sum">
								<view class="bold-num">{{ mealnousedDinnerEaten }}</view>
								<view>晚餐次数</view>
							</view>
						</view>
					</uni-collapse-item>
					<uni-collapse-item title="未报餐已用统计"
						thumb="https://limsapi.gzhbcss.com/static/tongji.png">
						<!-- 详情 -->
						<view class="rows" v-if="nomealusedlist.length > 0">
							<view class="col">日期</view>
							<block v-for="(menu, mIndex) in menu" :key="mIndex">
								<view class="menu col">
									{{ menu.name }}
								</view>
							</block>
						</view>
						<view class="row" v-for="(item,index) in nomealusedlist" :key="index">
							<view class="col">
								{{item.dutyDate.slice(5)}}
								<view style="color: #006400;">{{item.dayName}}</view>
							</view>
							<block v-for="(val, key, i) in item" :key="i">
								<template v-if="key != 'dutyDate' && key != 'dayName'">
									<view class="menu col">
										<view v-if="key == 'breakfastEaten' && val" style="color: #FF0000;">已用</view>
										<view v-if="key == 'lunchEaten' && val" style="color: #FF0000;">已用</view>
										<view v-if="key == 'dinnerEaten' && val" style="color: #FF0000;">已用</view>
									</view>
								</template>
							</block>
						</view>
						<view class="bccs">
							<view class="sum">
								<view class="bold-num">{{ nomealusedBreakfastEaten }}</view>
								<view>早餐次数</view>
							</view>
							<view class="sum">
								<view class="bold-num">{{ nomealusedLunchEaten }}</view>
								<view>中餐次数</view>
							</view>
							<view class="sum">
								<view class="bold-num">{{ nomealusedDinnerEaten }}</view>
								<view>晚餐次数</view>
							</view>
						</view>
					</uni-collapse-item>
				</uni-collapse>
			</uni-section>


		</view>


	</view>
</template>

<script>
	import {
		postFoodDietList, // 获取报餐信息
	} from "@/proxy/food/food.api.js";
	export default {
		data() {
			return {
				name: "",
				jobNumber: "",
				// 开始时间
				startDate: "",
				// 结束时间
				endDate: "",
				fooddietlist: [], // 报餐详情
				meallist:[], // 用餐统计
				nomealusedlist:[], // 未报餐已用餐统计
				mealnousedlist:[], // 报餐未用餐统计
				menu: [{
						name: "早餐"
					},
					{
						name: "中餐"
					},
					{
						name: "晚餐"
					},
				],
				dateValue: "",
				breakfastOrdered: 0, // 早餐报餐
				lunchOrdered: 0, // 中餐报餐
				dinnerOrdered: 0, // 晚餐报餐
				breakfastEaten: 0, // 早餐用餐
				lunchEaten: 0, // 中餐用餐
				dinnerEaten: 0, // 晚餐用餐
				mealnousedBreakfastEaten:0, // 报餐未用早餐次数
				mealnousedLunchEaten:0, // 报餐未用中餐次数
				mealnousedDinnerEaten:0, // 报餐未用晚餐次数
				nomealusedBreakfastEaten:0, // 未报餐已用早餐次数
				nomealusedLunchEaten:0, // 未报餐已用中餐次数
				nomealusedDinnerEaten:0, // 未报餐已用晚餐次数
			};
		},
		async mounted() {
			// 获取当前日期
			const currentDate = new Date();
			const year = currentDate.getFullYear();
			const month = String(currentDate.getMonth() + 1).padStart(2, '0'); // getMonth() 返回的是0-11，所以加1
			const formattedDate = `${year}-${month}`;
			this.dateValue = formattedDate;
			const {
				firstDay,
				lastDay
			} = this.getMonthFirstAndLastDay(formattedDate);
			this.startDate = firstDay;
			this.endDate = lastDay;

			const {
				name,
				jobNumber
			} = uni.getStorageSync("userInfo");
			this.name = name;
			this.jobNumber = jobNumber;

			await this.postFoodDietListData();
		},
		methods: {
			async onDateChange(e) {
				this.dateValue = e.detail.value;
				const {
					firstDay,
					lastDay
				} = this.getMonthFirstAndLastDay(this.dateValue);
				this.startDate = firstDay;
				this.endDate = lastDay;
				this.fooddietlist = []; // 报餐详情
				this.meallist = []; // 用餐统计
				this.nomealusedlist = []; // 未报餐已用餐统计
				this.mealnousedlist = []; // 报餐未用餐统计
				this.breakfastOrdered = 0; // 早餐报餐
				this.lunchOrdered = 0; // 中餐报餐
				this.dinnerOrdered = 0; // 晚餐报餐
				this.breakfastEaten = 0; // 早餐用餐
				this.lunchEaten = 0; // 中餐用餐
				this.dinnerEaten = 0; // 晚餐用餐
				this.mealnousedBreakfastEaten = 0; // 报餐未用早餐次数
				this.mealnousedLunchEaten = 0; // 报餐未用中餐次数
				this.mealnousedDinnerEaten = 0; // 报餐未用晚餐次数
				this.nomealusedBreakfastEaten = 0; // 未报餐已用早餐次数
				this.nomealusedLunchEaten = 0; // 未报餐已用中餐次数
				this.nomealusedDinnerEaten = 0; // 未报餐已用晚餐次数
				await this.postFoodDietListData();
			},
			getMonthFirstAndLastDay(dateStr) {
				// 将传入的年月字符串转为日期对象
				const [year, month] = dateStr.split('-').map(Number);

				// 获取该月的第一天
				const firstDay = new Date(year, month - 1, 1);

				// 获取该月的最后一天
				const lastDay = new Date(year, month, 0); // 月份+1，再设置天数为0，即得到上个月的最后一天

				// 格式化为 "yyyy-mm-dd"
				const formatDate = (date) => {
					const year = date.getFullYear();
					const month = String(date.getMonth() + 1).padStart(2, '0');
					const day = String(date.getDate()).padStart(2, '0');
					return `${year}-${month}-${day}`;
				};

				return {
					firstDay: formatDate(firstDay),
					lastDay: formatDate(lastDay)
				};
			},
			// 获取订餐详情
			async postFoodDietListData() {
				try {
					// 用户点击确认后执行的异步操作
					const result = await postFoodDietList({
						in: this.startDate,
						out: this.endDate,
						name: this.name, // 人
						jobNumber: this.jobNumber, // 
					});
					
					const daysOfWeek = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
					// console.log(result.data)
					if (result.code == 200) {
						// 循环遍历数组
						for (let i = 0; i < result.data.length; i++) {
							// 检查每个字段，如果字段值为 0，就累加
							this.breakfastOrdered += result.data[i].breakfastOrdered;
							this.lunchOrdered += result.data[i].lunchOrdered;
							this.dinnerOrdered += result.data[i].dinnerOrdered;
							this.breakfastEaten += result.data[i].breakfastEaten;
							this.lunchEaten += result.data[i].lunchEaten;
							this.dinnerEaten += result.data[i].dinnerEaten;
							
							const dutyDate = new Date(result.data[i].dutyDate);  // 将日期字符串转换为 Date 对象
							// 使用 getDay() 获取星期几
							const dayOfWeek = dutyDate.getDay();
							const dayName = daysOfWeek[dayOfWeek];
							if(result.data[i].breakfastOrdered == 1 || result.data[i].lunchOrdered == 1 || result.data[i].dinnerOrdered == 1){
								this.fooddietlist.push({
									dutyDate: result.data[i].dutyDate,
									breakfastOrdered: result.data[i].breakfastOrdered,
									lunchOrdered: result.data[i].lunchOrdered,
									dinnerOrdered: result.data[i].dinnerOrdered,
									dayName:dayName,
								})
							}
							// 用餐统计
							if(result.data[i].breakfastEaten == 1 || result.data[i].lunchEaten == 1 || result.data[i].dinnerEaten == 1){
								this.meallist.push({
									dutyDate: result.data[i].dutyDate,
									breakfastEaten: (result.data[i].breakfastEaten == 1) ? result.data[i].breakfastEaten : 0,
									lunchEaten: (result.data[i].lunchEaten == 1) ? result.data[i].lunchEaten : 0,
									dinnerEaten: (result.data[i].dinnerEaten == 1) ? result.data[i].dinnerEaten : 0,
									dayName:dayName,
								})
							}
							// 未报餐已用餐统计
							if((result.data[i].breakfastOrdered == 0 && result.data[i].breakfastEaten == 1) || (result.data[i].lunchOrdered == 0 && result.data[i].lunchEaten == 1) || (result.data[i].dinnerOrdered == 0 && result.data[i].dinnerEaten == 1)){
								this.nomealusedlist.push({
									dutyDate:result.data[i].dutyDate,
									breakfastEaten:  (result.data[i].breakfastOrdered == 0 && result.data[i].breakfastEaten == 1) ? result.data[i].breakfastEaten : 0,
									lunchEaten: (result.data[i].lunchOrdered == 0 && result.data[i].lunchEaten == 1) ? result.data[i].lunchEaten : 0,
									dinnerEaten: (result.data[i].dinnerOrdered == 0 && result.data[i].dinnerEaten == 1) ? result.data[i].dinnerEaten : 0,
									dayName:dayName,
								})
								
								this.nomealusedBreakfastEaten += (result.data[i].breakfastOrdered == 0 && result.data[i].breakfastEaten == 1) ? result.data[i].breakfastEaten : 0;
								this.nomealusedLunchEaten += (result.data[i].lunchOrdered == 0 && result.data[i].lunchEaten == 1) ? result.data[i].lunchEaten : 0;
								this.nomealusedDinnerEaten += (result.data[i].dinnerOrdered == 0 && result.data[i].dinnerEaten == 1) ? result.data[i].dinnerEaten : 0;
							}
							// 报餐未用餐统计
							if((result.data[i].breakfastOrdered == 1 && result.data[i].breakfastEaten == 0) || (result.data[i].lunchOrdered == 1 && result.data[i].lunchEaten == 0) || (result.data[i].dinnerOrdered == 1 && result.data[i].dinnerEaten == 0)){
								this.mealnousedlist.push({
									dutyDate: result.data[i].dutyDate,
									breakfastEaten: (result.data[i].breakfastOrdered == 1 && result.data[i].breakfastEaten == 0) ? result.data[i].breakfastOrdered : 0,
									lunchEaten: (result.data[i].lunchOrdered == 1 && result.data[i].lunchEaten == 0) ? result.data[i].lunchOrdered : 0,
									dinnerEaten: (result.data[i].dinnerOrdered == 1 && result.data[i].dinnerEaten == 0) ? result.data[i].dinnerOrdered : 0,
									dayName:dayName,
								})
								
								this.mealnousedBreakfastEaten += (result.data[i].breakfastOrdered == 1 && result.data[i].breakfastEaten == 0) ? result.data[i].breakfastOrdered : 0;
								this.mealnousedLunchEaten += (result.data[i].lunchOrdered == 1 && result.data[i].lunchEaten == 0) ? result.data[i].lunchOrdered : 0;
								this.mealnousedDinnerEaten += (result.data[i].dinnerOrdered == 1 && result.data[i].dinnerEaten == 0) ? result.data[i].dinnerOrdered : 0;			
							}
						}
					} else {
						this.$toast({
							title: result.data.msg,
						});
					}

				} catch (err) {
					this.$toast({
						icon: 'none',
						title: '发起失败，请联系管理员！',
						duration: 2000
					});
					console.log(err);
				}
			},
		}
	}
</script>

<style lang="scss">
	page {
		height: 100%;
		background: #F3F4F6;
	}

	.summary {
		.sort-time {
			display: flex;
			// margin: 0rpx 15rpx;
			// padding: 10rpx;
			height: 90rpx;
			border: 2rpx solid #eaeaea;
			// justify-content: space-between;
			background-color: #FFF;
			align-items: center;

			// border-radius: 10rpx;
			.select-month {
				flex: 1;
				height: 90rpx;
				line-height: 90rpx;
			}
		}

		.bctj {
			display: flex;
			align-items: center;
			padding: 20rpx 20rpx;
			line-height: 60rpx;
			position: relative;
			// margin-bottom: 20rpx;
			background-color: #FFF;

			.text-icon {
				width: 44rpx;
				height: 44rpx;
			}

			.tjtitle {
				flex: 1;
				font-size: 30rpx;
				color: #303133;
				margin-right: 10rpx;
				margin-left: 15rpx;
			}

			.tjtime {
				align-self: center;
				font-size: 30rpx;
				// color: #606266;
				margin-left: 10rpx;
			}

			&::after {
				position: absolute;
				z-index: 3;
				left: 0;
				right: 0;
				height: 0;
				content: '';
				bottom: 0;
				-webkit-transform: scaleY(0.5);
				transform: scaleY(0.5);
				border-bottom: 1px solid #E4E7ED;
				border-bottom-width: 1px;
				border-bottom-style: solid;
				border-bottom-color: rgb(228, 231, 237);
			}
		}

		.bccs {
			justify-content: space-around;
			display: flex;
			align-content: center;
			background: #fff;
			// border-radius: 10rpx;
			margin-bottom: 20rpx;

			.sum {
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
				flex: 1;
				margin: 30rpx 0;
				font-size: small;
				color: #75787d;

				.bold-num {
					font-weight: bold;
					font-size: 40rpx;
				}
			}
		}

		// .table {
		// 	border-radius: 15rpx;
		// 	background-color: #FFF;

		// 	.tabtr {
		// 		box-sizing: border-box;
		// 		display: flex;
		// 		justify-content: space-around;
		// 		position: relative;

		// 		.tabtd {
		// 			flex: 1;
		// 			text-align: center;
		// 			padding: 30rpx 20rpx;
		// 			font-size: 28rpx;
		// 			border-bottom: 1rpx #ebeef5 solid;
		// 			font-weight: 400;
		// 			color: #606266;
		// 		}

		// 		&::after {
		// 			content: '';
		// 			position: absolute;
		// 			left: 50%;
		// 			width: 1rpx;
		// 			height: 100%;
		// 			transform: translateX(-50%);
		// 			background-color: #ebeef5;
		// 		}

		// 		&:nth-child(2n + 3) {
		// 			background-color: #fafafa;
		// 		}
		// 	}
		// }
	}
	
	.rows {
		height: 80rpx;
		padding: 0rpx 30rpx;
	}
	
	.row {
		padding: 15rpx 30rpx;
	}
	
	.row,
	.rows {
		display: flex;
		border-bottom: 1px solid #ececec;
		align-items: center;
		background-color: #fff;
	
		.col {
			flex: 1;
			text-align: center;
			font-size: 29rpx;
		}
	}
</style>